package servlet;

import entity.Group;
import entity.User;
import serviceImpl.add;
import serviceImpl.createGroup;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CreateGroupServlet", value = "/CreateGroupServlet")
public class CreateGroupServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        String name=request.getParameter("name");
        String profile=request.getParameter("profile");
        String tags=request.getParameter("tags");

        User user =(User) request.getSession().getAttribute("user");

        System.out.println(name+"  "+profile+"   "+tags);

        Group g=new Group();
        g.setProfile(profile);
        g.setName(name);
        g.setTag(tags);
        g.setAnnouncement("无");
        g.setTask("无");


        createGroup a=new createGroup();

        String text="";
        if(a.add(g, user)==true){
            text="创建小组成功!您的小组id为"+g.getId();
        } else{
            text="小组已存在！";
        }
        PrintWriter out=response.getWriter();
        out.print(text);

    }
}
